import axios, { AxiosRequestConfig } from "axios";

type Siliconflow = {
  apiUrl: string;
  api: string;
};

export default class SiliconflowSdk {
  private config: Siliconflow;
  constructor(config: { apiUrl: string; api: string }) {
    if (!config.apiUrl) {
      throw Error("apiUrl 不存在");
    }
    if (!config.api) {
      throw Error("api 不存在");
    }
    this.config = config;
  }

  async exec(
    url: string,
    params: any,
    option?: {
      method?: "POST" | "GET";
    },
  ) {
    const { apiUrl, api } = this.config;

    const options: AxiosRequestConfig = {
      method: option?.method === "GET" ? "GET" : "POST",
      url: `${apiUrl}/${url}`,
      headers: { Authorization: `Bearer ${api}`, "Content-Type": "application/json" },
      data: JSON.stringify(params),
    };

    console.log(JSON.stringify(options, null, 2), "optionsoptionsoptions");

    const { data } = await axios(options);

    return data;
  }
}
